import {getCurrentInstance} from "vue";
import merge from "lodash/merge.js"
export const usePageCommonHook = (isForm= false)=>{
    const primaryKey = "surId"  // 主键

    const { proxy } = getCurrentInstance();
    const { sys_yes_no,fed_survey_type,fed_survey_state } = proxy.useDict('sys_yes_no','fed_survey_type','fed_survey_state');

    const columns = computed(()=>{
        const data = [
            {
                label: '问卷名称',
                prop: 'surName',
                hideInSearch:false,
                valueType:'input',
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    maxlength: 200,
                    modelModifiers: { trim: true },
                },
                orderInForm:999,
                propInForm:{
                    colProps:{
                        span:24
                    },
                },
                propInTable:{}
            },
            {
                label: '问卷描述',
                prop: 'surDesc',
                hideInSearch:true,
                valueType:'textarea',
                tableColumnProps: {
                    align: 'center'
                },
                orderInForm:888,
                fieldProps:{
                    maxlength: 200,
                    autosize: { minRows:6, maxRows: 10 },
                    modelModifiers: { trim: true },
                },
            },
            {
                label: '问卷类型',
                prop: 'surType',
                valueType:'select',
                hideInTable: true,
                hideInSearch: true,
                options:fed_survey_type.value,
                tableColumnProps: {
                    align: 'center'
                },
            },
            {
                label: '封面图片',
                prop: 'coverImg',
                orderInForm:777,
                hideInSearch:true,
                valueType: 'img',
                formatter:value=>import.meta.env.VITE_APP_WEB_URL + value,
                tableColumnProps: {
                    align: 'center'
                },
            },
            {
                label: '开始时间',
                prop: 'startTime',
                hideInSearch:true,
                hideInForm:true,
                valueType:'date-picker',
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    type: 'date',
                    format: "YYYY-MM-DD HH:mm:ss",
                    valueFormat: "YYYY-MM-DD HH:mm:ss",
                },
            },
            {
                label: '结束时间',
                prop: 'endTime',
                hideInSearch:true,
                hideInForm:true,
                valueType:'date-picker',
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    type: 'date',
                    format: "YYYY-MM-DD HH:mm:ss",
                    valueFormat: "YYYY-MM-DD HH:mm:ss"
                },
            },
            {
                label: '起止时间',
                prop: 'dateTimeRange',
                hideInSearch:true,
                hideInTable:true,
                valueType:'plus-date-picker',
                tableColumnProps: {
                    align: 'center'
                },
                colProps:{
                    span:24
                },
                fieldProps:{
                    type: 'datetime',
                    valueFormat: "YYYY-MM-DD HH:mm:ss"
                },
            },

            {
                label: '是否公开',
                tooltip:"公开则平台内所有用户可查看问卷",
                prop: 'isPublic',
                hideInSearch:true,
                valueType: isForm ? 'radio':'select',
                tableColumnProps: {
                    align: 'center'
                },
                options:sys_yes_no.value,
                optionsMap:{},
            },
            {
                label: '再次参与',
                tooltip:"点击提交后是否可以再次参与",
                prop: 'repeatJoin',
                hideInSearch:true,
                valueType:'radio',
                options: sys_yes_no.value,
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    maxlength: 200,
                    showWordLimit: true,
                    modelModifiers: { trim: true },
                    autosize: { minRows: 2, maxRows: 4 }
                },

            },
            {
                label: '状态',
                prop: 'state',
                hideInForm:true,
                valueType:'select',
                options: fed_survey_state.value,
                tableColumnProps: {
                    align: 'center'
                },
            },
            {
                label: '结束反馈语',
                prop: 'surFinish',
                hideInSearch:true,
                valueType:'textarea',
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    maxlength: 200,
                    autosize: { minRows:2, maxRows: 4 },
                    modelModifiers: { trim: true },
                },
            },
            {
                label: '备注',
                prop: 'remark',
                hideInSearch:true,
                hideInTable: true,
                valueType:'textarea',
                tableColumnProps: {
                    align: 'center'
                },
                fieldProps:{
                    maxlength: 200,
                    autosize: { minRows:2, maxRows: 4 },
                    modelModifiers: { trim: true },
                },
            },
            {
                label: '操作',
                prop: 'action',
                width: '180px',
                hideInSearch: true,
                hideInForm: true,
                tableColumnProps: {
                    align: 'center',
                    fixed: 'right'
                }
            }
        ];
        const result = data.map(item=>{
            let obj = {};
            if(isForm && item.propInTable){
                obj = { ...item.propInTable }
            }
            if(isForm && item.propInForm){
                obj = { ...item.propInForm }
            }
            return merge(item,obj)
        })
        if(isForm){
            return result.sort((a, b) => (b.orderInForm ||0) - (a.orderInForm || 0))
        }else{
            return result;
        }
    })

    return {
        sys_yes_no,
        fed_survey_type,
        fed_survey_state,
        proxy,
        columns,
        primaryKey
    }
}
